package com.oak.broke.websocket;


import com.oak.broke.websocket.rag.WebSocketProxyRagServerHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

@Configuration
@EnableWebSocket
public class ProxyWebSocketConfigurer implements WebSocketConfigurer {

    @Value("${ws.textMessage.limit:1024000}")
    private Integer textSize;
    @Value("${ws.binaryMessage.limit:1024000}")
    private Integer binarySize;

    @Autowired
    WebSocketProxyRagServerHandler webSocketProxyRagServerHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketProxyRagServerHandler, "/ws/**")
                .addInterceptors(new RagHandshakeInterceptor())
                .setAllowedOrigins("*");

    }

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        // ws 传输数据的时候，数据过大有时候会接收不到，所以在此处设置bufferSize
        container.setMaxTextMessageBufferSize(textSize);
        container.setMaxBinaryMessageBufferSize(binarySize);
        return container;
    }

}
